package com.sk.cardeal.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import com.sk.cardeal.R;


/**
 * Created by Administrator on 2017/10/18.
 */

public class FixedProportionImageView extends android.support.v7.widget.AppCompatImageView {

    private int radioHeight = 1;
    private int radiowidth = 1;
    private int width = 0;
    private int height = 0;
    private float radio = 1;
    private int realHeight = 0;
    private int realWidth = 0;

    private float left = 0,right = 0;

    public FixedProportionImageView(Context context) {
        this(context, null);
    }

    public FixedProportionImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FixedProportionImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FixedProportionImageView, defStyleAttr, 0);
        int n = array.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.FixedProportionImageView_iRadioWidth:
                    radiowidth = array.getInteger(attr, 1);
                    break;
                case R.styleable.FixedProportionImageView_iRadioHeight:
                    radioHeight = array.getInteger(attr, 1);
                    break;
                case R.styleable.FixedProportionImageView_left:
                    left =  array.getDimension(attr,0);
                    break;
                case R.styleable.FixedProportionImageView_right:
                    right = array.getDimension(attr,0);
                    break;
            }
        }

        radio = radiowidth / (float) radioHeight;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);

        if(mode == MeasureSpec.EXACTLY){
            width = size;
            realWidth = size;
            realHeight = (int) (realWidth / radio);
        }
        setMeasuredDimension(realWidth, realHeight);
    }
}
